package com.bmth.alise.client;

import com.bmth.alise.utils.conf.Configuration;
import com.bmth.alise.utils.rds.RedisUtils;

import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.scheduler.FileCacheQueueScheduler;

public class Bootstrap {

	public static void main(String[] args) {

		try {
			Configuration.init();
			RedisUtils.initPool();

			
			Spider spider = new Bootstrap().initSpider().scheduler(new FileCacheQueueScheduler("/Users/u01/workspace/tmp"));
			// 启动爬虫线程

			new SpiderRunnable(spider).start();
			
			// // 启动mq 接收url，添加到爬虫 线程
			// new MessageReceiver(Configuration.getProperty("MQUrl"),
			// Configuration.getProperty("UrlQuene"), spider)
			// .start();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Spider initSpider() {

		Integer RetryTimes = Integer.parseInt(Configuration.getProperty("RetryTimes"));
		Integer SleepTime = Integer.parseInt(Configuration.getProperty("SleepTime"));
		Integer TimeOut = Integer.parseInt(Configuration.getProperty("TimeOut"));
		String AcceptRegex = Configuration.getProperty("AcceptRegex");
		Integer ThreadNumber = Integer.parseInt(Configuration.getProperty("ThreadNumber"));

		Site site = Site.me().setRetryTimes(RetryTimes).setSleepTime(SleepTime).setTimeOut(TimeOut);

		// 初始化爬虫
		Spider spider = Spider.create(new CommonPageProcessor(site, AcceptRegex)).thread(ThreadNumber)
				.setUUID("search_it").setExitWhenComplete(false);

		return spider;
	}

	static class SpiderRunnable implements Runnable {
		Spider spider;

		SpiderRunnable(Spider spider) {
			this.spider = spider;
		}

		public void start() {
			new Thread(this, "Spider").start();
		}

		@Override
		public void run() {
			spider.start();
		}

	}
}
